package com.ruoyi.web.controller.websql;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.websql.domain.SqlDatasource;
import com.ruoyi.websql.domain.vo.SqlPageDataSourceVO;
import com.ruoyi.websql.model.ExecuteSql;
import com.ruoyi.websql.service.ISqlDatasourceService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * ***Controller
 * 
 * @author xudengyu
 * @date 2025-03-18
 */
@RestController
@RequestMapping("/websql/datasource")
public class SqlDatasourceController extends BaseController
{
    @Resource
    private ISqlDatasourceService sqlDatasourceService;

    @GetMapping("/checkConnectName/{dbName}")
    public AjaxResult checkConnectName(@PathVariable("dbName")String dbName){
        boolean f=sqlDatasourceService.checkConnectName(dbName);
        return success(f);
    }

    @PostMapping("/executeSqlNew")
    public AjaxResult executeSqlNew(@RequestBody ExecuteSql executeSql){
        return sqlDatasourceService.executeSqlNew(executeSql);
    }

    @GetMapping("/findSqlPageDataSourceList")
    public AjaxResult findSqlPageDataSourceList(){
        List<SqlPageDataSourceVO> sqlPageDataSourceVOList=sqlDatasourceService.findSqlPageDataSourceList();
        return success(sqlPageDataSourceVOList);
    }

    @PostMapping("/testDatasource")
    public AjaxResult testDatasource(@RequestBody SqlDatasource sqlDatasource){
        String msg=sqlDatasourceService.testDatasource(sqlDatasource);
        return msg.contains("连接失败")?AjaxResult.error(msg):AjaxResult.success(msg);
    }

    /**
     * 查询***列表
     */
    @PreAuthorize("@ss.hasPermi('websql:datasource:list')")
    @GetMapping("/list")
    public TableDataInfo list(SqlDatasource sqlDatasource)
    {
        startPage();
        List<SqlDatasource> list = sqlDatasourceService.selectSqlDatasourceList(sqlDatasource);
        return getDataTable(list);
    }

    /**
     * 导出***列表
     */
    @PreAuthorize("@ss.hasPermi('websql:datasource:export')")
    @Log(title = "***", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SqlDatasource sqlDatasource)
    {
        List<SqlDatasource> list = sqlDatasourceService.selectSqlDatasourceList(sqlDatasource);
        ExcelUtil<SqlDatasource> util = new ExcelUtil<SqlDatasource>(SqlDatasource.class);
        util.exportExcel(response, list, "***数据");
    }

    /**
     * 获取***详细信息
     */
    @PreAuthorize("@ss.hasPermi('websql:datasource:query')")
    @GetMapping(value = "/{dbId}")
    public AjaxResult getInfo(@PathVariable("dbId") Long dbId)
    {
        SqlDatasource sqlDatasource = new SqlDatasource();
        sqlDatasource.setID(dbId);
        return success(sqlDatasourceService.selectSqlDatasourceList(sqlDatasource).get(0));
    }

    /**
     * 新增***
     */
    @PreAuthorize("@ss.hasPermi('websql:datasource:add')")
    @Log(title = "***", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SqlDatasource sqlDatasource)
    {
        return toAjax(sqlDatasourceService.insertSqlDatasource(sqlDatasource));
    }

    /**
     * 修改***
     */
    @PreAuthorize("@ss.hasPermi('websql:datasource:edit')")
    @Log(title = "***", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SqlDatasource sqlDatasource)
    {
        return toAjax(sqlDatasourceService.updateSqlDatasource(sqlDatasource));
    }

    /**
     * 删除***
     */
    @PreAuthorize("@ss.hasPermi('websql:datasource:remove')")
    @Log(title = "***", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(sqlDatasourceService.deleteSqlDatasourceById(id));
    }
}
